package com.rules.admin.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gillion.grule.client.core.SpringCloudConfig;

import com.google.common.collect.Lists;
import com.rules.admin.dao.SpringCloudModelDao;
import com.rules.admin.entity.SpringCloudModel;
import com.rules.admin.service.SpringCloudService;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;

@Service
public class SpringCloudServiceImpl extends ServiceImpl<SpringCloudModelDao, SpringCloudModel> implements SpringCloudService {

   public IPage<SpringCloudModel> queryPage(SpringCloudModel springCloudModel, IPage<SpringCloudModel> page) {
      LambdaQueryWrapper<SpringCloudModel> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.eq(SpringCloudModel::getProjectId,springCloudModel.getProjectId());

      if (springCloudModel.getProjectId() != null && !"".equals(springCloudModel.getProjectId())) {
         queryWrapper.eq(SpringCloudModel::getProjectId,springCloudModel.getProjectId());
      }

      if (springCloudModel.getRequestMapping() != null && !"".equals(springCloudModel.getRequestMapping())) {
         queryWrapper.eq(SpringCloudModel::getRequestMapping,springCloudModel.getRequestMapping());

      }

      if (springCloudModel.getSpringCloudName() != null && !"".equals(springCloudModel.getSpringCloudName())) {
         queryWrapper.eq(SpringCloudModel::getSpringCloudName,springCloudModel.getSpringCloudName());

      }
      return this.page(page, queryWrapper);

   }

   public List<SpringCloudConfig> selectListByProjectCode(String projectCode) {

      LambdaQueryWrapper<SpringCloudModel> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.eq(SpringCloudModel::getProjectCode,projectCode);

      List<SpringCloudModel> springCloudModelList = this.list(queryWrapper);
      List<SpringCloudConfig> springCloudConfigList = Lists.newArrayList();
      Iterator iterator = springCloudModelList.iterator();

      while(iterator.hasNext()) {
         SpringCloudModel model = (SpringCloudModel)iterator.next();
         SpringCloudConfig springCloudConfig = new SpringCloudConfig();
         springCloudConfig.setSpringCloudName(model.getSpringCloudName());
         springCloudConfig.setRequestMapping(model.getRequestMapping());
         springCloudConfig.setRequestConfig(model.getRequestConfig());
         springCloudConfig.setRequestType(model.getRequestType());
         springCloudConfig.setRequestResult(model.getRequestResult());
         springCloudConfig.setParams(model.getParams());
         springCloudConfigList.add(springCloudConfig);
      }

      return springCloudConfigList;
   }
}
